<template>
  <div class="login">
    <div class="log"></div>
    <van-form @submit="onSubmit">
      <van-field
        v-model="username"
        name="name"
        label="用户名"
        placeholder="用户名"
        :rules="[{ required: true, message: '请填写用户名' }]"
      />
      <van-field
        v-model="password"
        type="password"
        name="pwd"
        label="密码"
        placeholder="密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <div style="margin: 16px;">
        <van-button round block type="info" native-type="submit">登入</van-button>
      </div>
      <div class="Jump">
        <a href="#"></a>
        <a href="#/register">去注册</a>
      </div>
    </van-form>
  </div>
</template>

<script>
// 提示信息
import { Toast } from 'vant'
export default {
  created() {
    this.localData = JSON.parse(localStorage.getItem("userData") || "[]")
  },
  data() {
    return {
      username: '',
      password: '',
      localData: []
    }
  },
  methods: {
    onSubmit(values) {
      if (this.localData.length < 1) {
        Toast('请先去注册!')
        return
      }
      for (let i = 0; i < this.localData.length; i++) {
        if (this.localData[i].name === values.name && this.localData[i].pwd === values.pwd) {
          Toast('登入成功!')
          // console.log(values)
          sessionStorage.setItem('userinfo', JSON.stringify(values))
          this.$router.push('/home')
          return
        } else {
          Toast('登入失败!')
        }
      }
    }
  }
}
</script>

<style lang="scss" scoped>
  .log{
    width: 100%;
    height: 77px;
   background: url('../../assets/home/logo.png') no-repeat center;
   background-size: 140px 40px;
   margin-bottom: 20px;
  }
  .Jump{
    display: flex;
    justify-content: space-between;
    padding: 0px 25px;
    text-decoration:underline
  }
</style>